//
//  DKViewController.m
//  小圈
//
//  Created by 许建勇 on 15/7/16.
//  Copyright (c) 2015年 HUB-ALEX. All rights reserved.
//

#import "DKViewController.h"
#import "DKReachable.h"
#import "DKView.h"



@interface DKInternetErrorLabel : DKView

@property (nonatomic,strong) UIImageView *errorIcon;

@property (nonatomic,strong) UILabel *errorLabel;

@end

@implementation DKInternetErrorLabel
- (void)initParameter
{
    [self.layer setBorderWidth:0.5f];
    [self.layer setBorderColor:[UIColor colorWithRed:221/255.0
                                               green:204/255.0
                                                blue:185/255.0
                                               alpha:1.0].CGColor];
    [self setBackgroundColor:[UIColor colorWithRed:255/255.0
                                             green:240/255.0
                                              blue:224/255.0
                                             alpha:1.0]];
}

- (void)initUI
{
    _errorIcon=[[UIImageView alloc] init];
    [_errorIcon setImage:[UIImage imageNamed:@"offline"]];
    [_errorIcon setContentMode:UIViewContentModeScaleAspectFit];
    [self addSubview:_errorIcon];
    
    _errorLabel=[[UILabel alloc] init];
    [_errorLabel setText:@"当前网络不可用，请检查你的网络"];
    [_errorLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [_errorLabel setTextColor:[UIColor colorWithRed:72/255.0
                                              green:72/255.0
                                               blue:72/255.0
                                              alpha:1.0]];
    [self addSubview:_errorLabel];
}

- (void)refreshLayout:(CGRect)frame
{
    _errorIcon.frame=CGRectMake(10,
                                0,
                                20,
                                frame.size.height);
    
    _errorLabel.frame=CGRectMake(_errorIcon.frame.origin.x+_errorIcon.frame.size.width+5,
                                 0,
                                 frame.size.width-(_errorIcon.frame.origin.x+_errorIcon.frame.size.width+5),
                                 frame.size.height);
}

@end











@interface DKViewController()
/**
 *  记录上一次frame的形状
 */
@property (nonatomic,assign) CGRect previousFrame;


/**
 *  是否显示网络状态变化条
 */
@property (nonatomic,assign) BOOL showConnectingError;

/**
 *  网络不可用的提示
 */
@property (nonatomic,strong) DKInternetErrorLabel *internetErrorLabel;

@end

@implementation DKViewController

#pragma mark - Init
-(instancetype)init
{
    self=[super init];
    if(self)
    {
        
    }
    return self;
}


#pragma mark - Getter
- (NSMutableDictionary *)info
{
    if(_info==nil)
    {
        _info=[[NSMutableDictionary alloc] init];
    }
    return _info;
}

- (DKInternetErrorLabel *)internetErrorLabel
{
    if(_internetErrorLabel==nil)
    {
        _internetErrorLabel=[[DKInternetErrorLabel alloc] init];
    }
    return _internetErrorLabel;
}


#pragma mark - Setter
- (void)setShowConnectingError:(BOOL)showConnectingError
{
    _showConnectingError=showConnectingError;
    
    if(_showConnectingError)
    {
        if(self.internetErrorLabel.superview==nil)
        {
            [self.view addSubview:self.internetErrorLabel];
        }
    }
    else
    {
        [self.internetErrorLabel removeFromSuperview];
    }
}



#pragma mark - Super
/**
 *  页面布局改变
 */
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    /*
    //网络状态标示始终在最前方
    if(_internetErrorLabel!=nil && _internetErrorLabel.superview!=nil)
    {
        [self.view bringSubviewToFront:_internetErrorLabel];
        NSLog(NSStringFromCGRect(self.navigationController.navigationBar.frame));
        NSLog(NSStringFromCGRect(self.view.frame));
        
        _internetErrorLabel.frame=CGRectMake(0,
                                             64-self.view.frame.origin.y,
                                             self.view.frame.size.width,
                                             40);
    }
    */
    [self checkLayout];
}

/**
 *  添加通知+监听
 *
 *  @param animated 系统参数
 */
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    /*
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(listenInternetState:)
                                                 name:kReachableNotificationReachable
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(listenInternetState:)
                                                 name:kReachableNotificationUnreachable
                                               object:nil];
     */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDisappear)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    //[MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    //[MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self vcDealloc];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
   
    //设定背景色
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //数值初始化
    _previousFrame=CGRectZero;
    _keyboardHeight=0;
    _canShowInternetState=NO;
    
    //初始化参数+UI
    [self initParameter];
    [self initUI];
    //服务器加载数据
    [self initData];
    
    /*
    if(![DKReachable shareReachble].isReachable && self.canShowInternetState)
    {
        self.showConnectingError=YES;
    }
    else
    {
        self.showConnectingError=NO;
    }
     */
}



#pragma mark - Action
/**
 *  检查布局是否需要改变
 */
-(void)checkLayout
{
    CGRect reviseFrame=self.view.frame;//修正的frame
    reviseFrame.size.height-=_keyboardHeight;
    if(CGRectEqualToRect(_previousFrame, reviseFrame))
    {//形状相同，则不通知刷新布局
        
    }
    else
    {//形状不同，通知刷新布局，且记录新布局
        _previousFrame=reviseFrame;
        [self refreshLayout:_previousFrame];
    }
}

/**
 *  键盘弹起时触发
 *
 *  @param notification 通知
 */
-(void)keyboardAppear:(NSNotification*)notification
{
    CGRect keyboardBounds;
    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
    _keyboardHeight=keyboardBounds.size.height;
    [self checkLayout];
}

/**
 *  键盘收起时触发
 */
-(void)keyboardDisappear
{
    _keyboardHeight=0;
    [self checkLayout];
}

/**
 *  网络状况变化通知监听
 *
 *  @param notificaiton 通知
 */
- (void)listenInternetState:(NSNotification *)notificaiton
{
    if([notificaiton.name isEqualToString:kReachableNotificationReachable])
    {
        self.showConnectingError=NO;
    }
    else if([notificaiton.name isEqualToString:kReachableNotificationUnreachable])
    {
        if(self.canShowInternetState)
        {
            self.showConnectingError=YES;
        }
    }
}







#pragma mark - 扩展方法

/**传参初始化*/
-(instancetype)initWithMutableInfo:(NSMutableDictionary*)info
{
    self=[super init];
    if(self)
    {
        _info=info;
    }
    return self;
}

/**传参初始化*/
-(instancetype)initWithInfo:(NSDictionary*)info
{
    self=[super init];
    if(self)
    {
        _info=[NSMutableDictionary dictionaryWithDictionary:info];
    }
    return self;
}

- (void)refreshLayout
{
    CGRect reviseFrame=self.view.frame;//修正的frame
    reviseFrame.size.height-=_keyboardHeight;
    _previousFrame=reviseFrame;
    [self refreshLayout:_previousFrame];
}

/**初始化UI界面*/
-(void)initUI
{
    
}

/**初始化数值参数*/
-(void)initParameter
{
    
}

/**
 *  从服务器下载数据
 */
-(void)initData
{
    
}

/**页面布局调整*/
-(void)refreshLayout:(CGRect)frame
{
    
}


/**
 *  控制器销毁时调用
 */
- (void)vcDealloc
{
    
}
@end
